//
//  SEGFriendDetailViewController.m
//  ExcellentHome
//
//  Created by 陈区 on 13-11-21.
//  Copyright (c) 2013年 陈区. All rights reserved.
//

#import "SEGFriendDetailViewController.h"
//#import "MessageDataBaseModel.h"
//#import "MessageModel.h"
//#import "SEGSessionMessageViewController.h"
//#import "UILabel+Extra.h"

@interface SEGFriendDetailViewController ()

@end

@implementation SEGFriendDetailViewController
//@synthesize myFriend;
- (id)initWithNibName:(NSString*)nibNameOrNil bundle:(NSBundle*)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)loadView
{
    UIScreen* scr = [UIScreen mainScreen];
    CGRect appbounds = CGRectMake(0, 0, scr.bounds.size.width, scr.bounds.size.height - kNavigationBarAndStatusBarHeight);
    UIView* mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, appbounds.size.width, appbounds.size.height)];

    CGRect tabbounds = CGRectMake(0, 0, scr.bounds.size.width, scr.bounds.size.height - kNavigationBarAndStatusBarHeight - 44);
    m_tableView = [[UITableView alloc] initWithFrame:tabbounds style:UITableViewStylePlain];
    m_tableView.delegate = self;
    m_tableView.dataSource = self;
    m_tableView.backgroundView = nil;
    m_tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    m_tableView.backgroundColor = [UIColor clearColor];

    [mainView addSubview:m_tableView];

    UIView* heardView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scr.bounds.size.width, 145)];

    UIImageView* icon = [[UIImageView alloc] initWithFrame:CGRectMake((heardView.frame.size.width - 65) / 2, 30, 65, 65)];
    icon.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")];
    icon.tag = 1011;
    icon.layer.masksToBounds = YES;
    icon.layer.cornerRadius = 5.f;
    [heardView addSubview:icon];

    UILabel* lblName = [[UILabel alloc] initWithFrame:CGRectMake((heardView.frame.size.width - 320) / 2, icon.frame.origin.y + icon.frame.size.height + 10, 320, FONT_LIST_AND_DETAIL_SIZE)];
    lblName.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
    lblName.backgroundColor = [UIColor clearColor];
    lblName.textAlignment = NSTextAlignmentCenter;
    lblName.textColor = kColor_2;
    lblName.tag = 1012;
    [heardView addSubview:lblName];

    m_tableView.tableHeaderView = heardView;

    UIView* footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, scr.bounds.size.width, 74)];

    UIButton* outBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    outBtn.frame = CGRectMake(10, 30, (heardView.frame.size.width - 20), 44);
    [outBtn setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_green")] forState:UIControlStateNormal];
    [outBtn setBackgroundImage:[UIImage imageWithContentsOfFile:SEGPicName(@"btn_green")] forState:UIControlStateHighlighted];
    [outBtn addTarget:self action:@selector(btnSendMessageClicked:) forControlEvents:UIControlEventTouchUpInside];
    outBtn.titleLabel.textColor = kColor_2;
    outBtn.hidden = NO;
    [outBtn setTitle:NSLocalizedString(@"发送消息", nil) forState:UIControlStateNormal];
    [outBtn.titleLabel setFont:[UIFont systemFontOfSize:20]];
    [footerView addSubview:outBtn];
    m_tableView.tableFooterView = footerView;

    self.view = mainView;
}

//- (void)addFriendEntityData:(FriendEntity *)aFriend
//{
//    if (self.myFriend!=aFriend) {
//
//        self.myFriend=nil;
//        self.myFriend=aFriend;
//    }
//    UILabel *nameLab=(UILabel*)[self.view viewWithTag:1012];
//    nameLab.text=self.myFriend.nickName;
//
//    UIImageView *iconView=(UIImageView*)[self.view viewWithTag:1011];
//    if(myFriend.portraitLocalPath)
//    {
//        NSString *filePath = [SystemUtils mideaFilePathForFileName:myFriend.portraitLocalPath];
//        iconView.image = [UIImage imageWithContentsOfFile:filePath];
//
//    }else
//    {
//        iconView.image=[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82.png")];
//    }
//    UITextView *descLab=(UITextView*)[self.view viewWithTag:1021];
//    descLab.text=self.myFriend.desc;
//
//
//}

// 设置数据
- (void)setFriendId:(NSString*)friendId
{
    [self requestFriendDetailWithUserId:friendId finished:^(id result, NSError* error) {
        NSDictionary* resultDict = result;
        NSString* code = [resultDict objectForKey:@"code"];
        if ([code isEqualToString:@"0"]) {
            self.friendModel = [FriendModel initWithDictionary:[resultDict objectForKey:@"data"]];

            UIImageView* iconView = (UIImageView*)[self.view viewWithTag:1011];
            if (self.friendModel.ico) {
                NSString* iconUrl = [NSString stringWithFormat:@"%@%@", kClientImageRootURL, self.friendModel.ico];
                [iconView setImageWithURL:[NSURL URLWithString:iconUrl] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")] options:SDWebImageLowPriority];
            }
            else {
                iconView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")];
            }

            [m_tableView reloadData];
        }
    }];
}

/**
 *  请求朋友详情
 *
 *  @param finished 网络请求完成后的操作
 */
- (void)requestFriendDetailWithUserId:(NSString *)userId finished:(SEGNetworkRequestCallBack)finished
{
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:userId,@"userId", nil];
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/",kUhomecp_app,@"/userInfo/userMailDetail.json",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        NSMutableDictionary *responseDic = [NSMutableDictionary dictionaryWithDictionary:result];
        
        NSString *code = [responseDic objectForKey:@"code"];
        if(![code isKindOfClass:[NSString class]])
        {//防止code为NSNull类型时崩溃
            code = @"-1";
        }
        //数据返回正常
        finished(result,nil);
    }];
}

- (void)btnSendMessageClicked:(UIButton*)sender
{
    NSString* fromId = [NSString stringWithFormat:@"%@|%@|1", self.friendModel.userId, kAccountUserID];
    NSDictionary *param = @{@"fromId"       : fromId ?: @"",
                            @"title"        : self.friendModel.nickname ?: @"",
                            @"sender"       : self.friendModel.nickname ?: @"",
                            @"sendUserId"   : self.friendModel.userId ?: @""
                            
      };
    
    UIViewController *messageVC = [[SEGUhomeMediator sharedInstance] seg_initOldActivitySessionMessageViewController:param];
    [self.navigationController pushViewController:messageVC animated:YES];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"详细资料";
    [self showCustomNavigationBackButton];
}

- (void)onModifyFriendPotraitResponse_UI:(NSNotification*)aNotification
{
    NSDictionary* userInfo = [aNotification userInfo];

    if ([self.friendModel.userId isEqualToString:[userInfo objectForKey:@"EntityID"]]) {

        UIImageView* iconView = (UIImageView*)[self.view viewWithTag:1011];
        NSData* imageData = [userInfo objectForKey:@"ImageData"];
        if (imageData) {
            iconView.image = [UIImage imageWithData:imageData];
        }
    }
}
#pragma mark - tableview datasource
- (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView
{

    return 1;
}

- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section
{

    return 2;
}

//每行的高度
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{

    return 44;
}
#pragma mark - tableview delegate
- (UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSString* indentSt = [NSString stringWithFormat:@"cellsectionrow%ld", (long)indexPath.row];

    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentSt];
    switch (indexPath.row) {

    case 0: {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentSt];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.contentView.backgroundColor = [UIColor clearColor];

            UIImageView* lineImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 0.5)];

            lineImgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
            [cell.contentView addSubview:lineImgView];

            UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 50, FONT_LIST_AND_DETAIL_SIZE)];
            name.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
            name.backgroundColor = [UIColor clearColor];
            name.textAlignment = NSTextAlignmentLeft;
            name.text = @"呢称";
            name.textColor = kColor_2;
            [cell.contentView addSubview:name];

            UILabel* lblName = [[UILabel alloc] initWithFrame:CGRectMake(name.frame.origin.x + name.frame.size.width + 20, 12, 200, FONT_LIST_AND_DETAIL_SIZE)];
            lblName.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
            lblName.backgroundColor = [UIColor clearColor];
            lblName.tag = 1020;
            lblName.textAlignment = NSTextAlignmentLeft;
            //                lblName.text = self.friendModel.nickname;
            lblName.textColor = kColor_2;
            [cell.contentView addSubview:lblName];

            UIImageView* lineImgView1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, 43, self.view.bounds.size.width, 0.5)];

            lineImgView1.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
            [cell.contentView addSubview:lineImgView1];
        }
        UILabel* nameLab = (UILabel*)[self.view viewWithTag:1020];
        nameLab.text = self.friendModel.nickname;

        return cell;
        break;
    }
    case 1: {
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentSt];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.contentView.backgroundColor = [UIColor clearColor];

            UILabel* name = [[UILabel alloc] initWithFrame:CGRectMake(20, 12, 50, FONT_LIST_AND_DETAIL_SIZE + 5)];
            name.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
            name.backgroundColor = [UIColor clearColor];
            name.textAlignment = NSTextAlignmentLeft;
            name.text = @"描述";
            name.textColor = kColor_2;
            [cell.contentView addSubview:name];

            UILabel* lblName = [[UILabel alloc] initWithFrame:CGRectMake(name.frame.origin.x + name.frame.size.width + 20, 12, 200, FONT_LIST_AND_DETAIL_SIZE + 5)];
            lblName.font = [UIFont systemFontOfSize:FONT_LIST_AND_DETAIL_SIZE];
            lblName.backgroundColor = [UIColor clearColor];
            lblName.tag = 1021;
            lblName.textAlignment = NSTextAlignmentLeft;
            lblName.textColor = kColor_2;
            [cell.contentView addSubview:lblName];

            UIImageView* lineImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 43, self.view.bounds.size.width, 0.5)];

            lineImgView.image = [UIImage imageWithContentsOfFile:SEGPicName(@"line_640")];
            [cell.contentView addSubview:lineImgView];
        }
        UITextView* descLab = (UITextView*)[self.view viewWithTag:1021];
        if (self.friendModel.desc.length > 0) {
            descLab.text = self.friendModel.desc;
        }
        else {
            descLab.text = @"这个邻居什么都没有写";
        }
    }
        return cell;
        break;

    default:
        break;
    }

    return cell;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
